package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DIshMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.*;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DIshMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 保存菜品信息同时保存口味信息
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveDishWithFlavor(DishDto dishDto) {
        //将菜品信息保存到数据库中
        this.save(dishDto);
        Long dishId = dishDto.getId();//菜品id
        //将菜品id保存到DishFlavor中
        //通过for方式实现
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }
        //通过stream流方式实现
        /*List<DishFlavor> flavors = dishDto.getFlavors();
        flavors =flavors.stream().map((item)->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
*/
        //将菜品的口味信息保存到数据库中
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询菜品及其口味信息,(用于修改菜品信息时的数据回显)
     * @param id
     * @return
     */
    @Override
    public DishDto getByDishWithDisFlavor(Long id) {
        //根据菜品id查询出来菜品信息
        Dish dis = this.getById(id);
        //获取一个DishDto对象
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dis,dishDto);
        //根据菜品id查询口味表
        //构建查询对象
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id!=null, DishFlavor::getDishId,dis.getId());
        queryWrapper.orderByDesc(DishFlavor::getUpdateTime);
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        //将口味信息写进dishDto中
       dishDto.setFlavors(list);
        return dishDto;
    }

    /**
     * 修改菜品信息
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateByDIshWithDishFlavor(DishDto dishDto) {
        //修改菜品信息
        this.updateById(dishDto);
        //修改口味信息
        //首先清空口味表中菜品对应的口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dishDto.getId()!= null,DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //再插入口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        /*for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }*/
        flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 批量修改菜品起售状态
     * @param status
     * @param ids
     */
    @Override
    public void updateStatus(Integer status,List<Long> ids) {
        //首先根据ids查询dish表
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        List<Dish> list = this.list(queryWrapper);
        //将list中的status属性赋值
        List <Dish> dishList = list.stream().map((item)->{
            item.setStatus(status);
            return item;
        }).collect(Collectors.toList());
        //再将赋值完的dishList保存到数据库中
        this.updateBatchById(dishList);

    }

    /**
     * 批量删除菜品信息
     * @param ids
     */
    @Override
    public void deleteByIds(List<Long> ids) {
        //首先查询菜品状态是否为停售状态
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        queryWrapper.eq(Dish::getStatus,1);
        int count = this.count(queryWrapper);
        if (count>0){
            throw new CustomException("菜品正在售卖中，不能删除");
        }
        //再删除数据库
        this.removeByIds(ids);
    }
}
